package com.shitou.shitouproduct.controller;

import java.util.Arrays;
import java.util.Map;

import com.shitou.shitoucommons.annotation.CheckUserLogin;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.shitou.shitouproduct.entity.AttrEntity;
import com.shitou.shitouproduct.service.AttrService;
import com.shitou.shitoucommons.utils.PageUtils;
import com.shitou.shitoucommons.utils.RespBean;



/**
 * 商品属性
 *
 * @author 石头人
 * @email 3076767823@qq.com
 * @date 2023-03-04 18:03:45
 */
@RestController
@RequestMapping("shitouproduct/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    public RespBean list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return RespBean.success().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public RespBean info(@PathVariable("attrId") Long attrId){
        AttrEntity attr = attrService.getById(attrId);

        return RespBean.success().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public RespBean save(@RequestBody AttrEntity attr){
        attrService.save(attr);

        return RespBean.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public RespBean update(@RequestBody AttrEntity attr){
        attrService.updateById(attr);

        return RespBean.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public RespBean delete(@RequestBody Long[] attrIds){
        attrService.removeByIds(Arrays.asList(attrIds));

        return RespBean.success();
    }



    /**
     * 用户通过分类id获取该类的所有属性
     * @param categoryId 分类id
     */
    @ApiOperation(value = "通过分类的id获取该分类的所有属性，需要登录校验")
    @CheckUserLogin
    @PostMapping("/attrByCategoryId")
    public RespBean attrByCategoryId(int categoryId){
        return attrService.attrByCategoryId(categoryId);
    }


    /**
     * 用户通过分类id获取该类的所有属性
     * @param categoryId 分类id
     */
    @ApiOperation(value = "通过分类的id获取该分类的所有属性，需要登录校验")
    @PostMapping("/base/list")
    public RespBean bastAttrByCategoryId( @RequestParam Map<String, Object> params, @RequestParam Long categoryId){
        PageUtils page = attrService.baseList(params, categoryId);

        return RespBean.success().put("page", page);
    }
}
